package org.elasticsearch.plugin.nlpcn;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestStatus;
import org.nlpcn.es4sql.SearchDao;
import org.nlpcn.es4sql.query.SqlElasticRequestBuilder;


public class RestSqlAction extends BaseRestHandler {

	@Inject
	public RestSqlAction(Settings settings, Client client, RestController restController) {
		super(settings, restController, client);
		restController.registerHandler(RestRequest.Method.POST, "/_sql/_explain", this);
		restController.registerHandler(RestRequest.Method.GET, "/_sql/_explain", this);
		restController.registerHandler(RestRequest.Method.POST, "/_sql", this);
		restController.registerHandler(RestRequest.Method.GET, "/_sql", this);
	}

	@Override
	protected void handleRequest(RestRequest request, RestChannel channel, final Client client) throws Exception {
		String sql = request.param("sql");

		if (sql == null) {
			sql = request.content().toUtf8();
		}
		SearchDao searchDao = new SearchDao(client);
		 SqlElasticRequestBuilder actionRequestBuilder = searchDao.explain(sql);
	     ActionRequest actionRequest = actionRequestBuilder.request();
			// TODO add unittests to explain. (rest level?)
	        if (request.path().endsWith("/_explain")) {
				String jsonExplanation = actionRequestBuilder.explain();
				BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, jsonExplanation);
				channel.sendResponse(bytesRestResponse);
			} else {
				new ActionRequestRestExecuter(actionRequestBuilder, channel, client).execute();
			}
	        
//        QueryAction queryAction= searchDao.explain(sql);
//
//		// TODO add unittests to explain. (rest level?)
//		if (request.path().endsWith("/_explain")) {
//			String jsonExplanation = queryAction.explain().explain();
//			BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, jsonExplanation);
//			channel.sendResponse(bytesRestResponse);
//		} else {
//            Map<String, String> params = request.params();
//            RestExecutor restExecutor = ActionRequestRestExecuterFactory.createExecutor(params.get("format"));
//			restExecutor.execute(client,params,queryAction,channel);
//		}
	}
}